/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.helloactivity;

import java.lang.reflect.Method;

import org.sourceprojects.lycia.DefinitionBuilder;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.ParserContext;
import org.sourceprojects.lycia.QualifiedName;
import org.sourceprojects.lycia.annotations.Parent;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.w3c.dom.Element;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;

/**
 * A minimal "Hello, World!" application.
 */
public class HelloActivity extends Activity {
	private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><test id=\"Hello from Xml\" /></html>";

	public HelloActivity() {
	}

	/**
	 * Called with the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set the layout for this activity. You can find it
		// in res/layout/hello_activity.xml
		setContentView(R.layout.hello_activity);

		try {
			Method m = HelloActivity.class.getDeclaredMethod("test",
					Element.class, Element.class, ParserContext.class);

			FluentBuilder<String> builder = prepare();
			LyciaParser<String> parser = builder.makeAndroidCompatible()
					.parser(tag("test", method(m, this))).build();

			parser.parse(XML);

			String text = parser.getParserContext().getContextObject();
			Log.d("HelloActivity", text);
		} catch (LyciaParsingException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}

	public void test(Element element, @Parent Element parent,
			ParserContext<String> parserContext) {

		String value = element.getAttribute("id");
		parserContext.setContextObject(value + ": " + element.getLocalName()
				+ " -> " + parent.getLocalName());
	}
}
